package com.rs.statistics.service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.rs.statistics.dao.IMemberDao;
import com.rs.statistics.dto.MemberDto;
import com.rs.statistics.model.Member;

@Service
public class MemberService implements IMemberService {
	@Resource
	private IMemberDao memberDao;

	@Override
	public Map findAll(MemberDto dto) {
		Pageable pageable = PageRequest.of(dto.getPage()-1,dto.getLimit());
		Map<String,Object> map = new HashMap<String,Object>();
		//List<Member> list = memberDao.findAll(getWhereClause(dto));
		Page<Member> page = memberDao.findAll(getWhereClause(dto), pageable);
		map.put("code", 0);
		map.put("count", memberDao.count(getWhereClause(dto)));
		map.put("data", page.getContent());
		return map;

	}

	private Specification<Member> getWhereClause(MemberDto dto) {
		return new Specification<Member>() {

			@Override
			public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				
				List<Predicate> list = new ArrayList<Predicate>();
				/**
				 * // 姓名
				 */
				if(dto.getName()!=null && ""!=dto.getName()) {
					list.add(criteriaBuilder.like(root.<String>get("name"), "%"+dto.getName()+"%"));
				}
				/**
				 * 电话
				 */
				if(dto.getPhone()!=null && ""!=dto.getPhone()) {
					list.add(criteriaBuilder.like(root.<String>get("phone"), "%"+dto.getPhone()+"%"));
				}
				/**
				 * 会员卡号
				 */
				if(dto.getCard_no()>0) {
					list.add(criteriaBuilder.equal(root.get("card_no").as(Integer.class), dto.getCard_no()));
				}
				
				/**
				 * 起始时间
				 */
				if(dto.getMinregdate()!=null) {
					list.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"), dto.getMinregdate()));//大于等于起始日期
					}
				/**
				 * 结束时间
				 */
				if(dto.getMaxregdate()!=null) {
					list.add(criteriaBuilder.lessThanOrEqualTo(root.get("regdate").as(Date.class), dto.getMaxregdate()));//小于等于截止日期
					}
				/**
				 *  资源使用状态
				 */
				if(dto.getStatus()>0) {
					list.add(criteriaBuilder.equal(root.get("status").as(Integer.class), dto.getStatus()));
				}
				/**
				 * 系
				 */
				if(dto.getDepartment()!=null && ""!=dto.getDepartment()) {
					list.add(criteriaBuilder.like(root.<String>get("department"), "%"+dto.getDepartment()+"%"));
				}
				/**
				 * 专业
				 */
				if(dto.getProfession()!=null && ""!=dto.getProfession()) {
					list.add(criteriaBuilder.like(root.<String>get("profession"), "%"+dto.getProfession()+"%"));
				}
				/**
				 * 年级
				 */
				if(dto.getProfession()!=null && ""!=dto.getProfession()) {
					list.add(criteriaBuilder.like(root.<String>get("profession"), "%"+dto.getProfession()+"%"));
				}
				Predicate[] pre = new Predicate[list.size()];
				return query.where(list.toArray(pre)).getRestriction();
			}
			
		};
	}

}
